# coding=utf-8

import web
from web.utils import storage

from db import connection

db = connection.default

class Departure(object):
    """
    出发地相关的前台数据接口
    """
    @staticmethod
    def get_departs():
        """
        获取所有的出发城市
        """
        return db.select('travel_depart'
                , where = 'parent_id = 0'
                , order = 'id ASC')

class Partner(object):
    """
    合作商家相关的前台数据接口
    """
    @staticmethod
    def get_partner_by_id(id):
        """
        获取给定id的合作商家信息
        """
        sets = db.select('partner', where = 'id = $id', vars = locals())
        if sets:
            return sets[0]
        return None

    @staticmethod
    def get_partners(offset, length):
        """
        分页获取合作商家
        """
        sets = db.query('''
            SELECT SQL_CALC_FOUND_ROWS * 
            FROM partner 
            ORDER BY sort DESC 
            LIMIT $offset, $length;
            '''
            , vars = locals())
        totals = db.query('SELECT FOUND_ROWS() AS totals;')
        return storage(result = sets, totalrecords = totals[0].totals)
